package com.dycn.posdashboard.util;

import java.util.Calendar;
import java.util.Date;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;

public class DateJsonValueProcessor implements JsonBeanProcessor {

	public JSONObject processBean(Object bean, JsonConfig arg1) {
		JSONObject jsonObject = null;
		if (bean instanceof java.sql.Date) {
			bean = new Date(((java.sql.Date) bean).getTime());

		}
		if (bean instanceof java.sql.Timestamp) {
			bean = new Date(((java.sql.Timestamp) bean).getTime());
		}
		if (bean instanceof Date) {
			jsonObject = new JSONObject();
			
			if( bean instanceof Date ){
		         Calendar c = Calendar.getInstance();
		         c.setTime( (Date) bean );
		         jsonObject = new JSONObject().element( "year", c.get( Calendar.YEAR ) )
		               .element( "month", c.get( Calendar.MONTH ) )
		               .element( "day", c.get( Calendar.DAY_OF_MONTH ) )
		               .element( "hours", c.get( Calendar.HOUR_OF_DAY ) )
		               .element( "minutes", c.get( Calendar.MINUTE ) )
		               .element( "seconds", c.get( Calendar.SECOND ) )
		               .element( "milliseconds", c.get( Calendar.MILLISECOND ) );
		         
		         
		      }else{
		         jsonObject = new JSONObject( true );
		      }
		} else {
			jsonObject = new JSONObject(true);
		}
		return jsonObject;
	}
}
